package com.education.basic.action;

import com.education.basic.entity.Administrator;
import com.education.basic.entity.Score;
import com.education.basic.entity.Teacher;
import com.education.basic.resource.Courses;
import com.education.basic.service.ScoreService;
import com.education.basic.service.TeacherService;
import com.yunshangwoniu.core.baseAction.BaseAction;
import com.yunshangwoniu.core.cache.CacheAble;
import com.yunshangwoniu.core.page.Page;
import com.yunshangwoniu.core.utils.Logger;
import com.yunshangwoniu.core.utils.RequestUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@Scope("prototype")
@Namespace("/score")
@ParentPackage(value="struts-default")
public class ScoreAction extends BaseAction
{
    /**
	 * 
	 */
	private static final long serialVersionUID = -5296144845951870045L;
	@Autowired
    private ScoreService scoreService;
    private Map<String,List<Courses>> tree;
    @Resource
    private CacheAble<Object, Object> cacheAble;
    @Autowired
    private TeacherService teacherService;
    private String code;
    private List<Score> scores;
    private long id;
    private double score;


    @Action(value="list",results={@Result(name="success",location="list.jsp")})
	public String list()
    {
        Administrator ad = (Administrator) cacheAble.get(RequestUtils.getIp(getRequest()));
        String empno = "";
        if(ad!=null&&ad.getLevel()==1)//超级管理员
        {
            Logger.debug(this.getClass(),"current administrator is super admininistrator so it can do all privileges!!! ");
        }else if(ad!=null&&ad.getLevel()==4)
        {
            Logger.debug(this.getClass(),"current administrator is Temporary administrator ");
            empno = "null";
        }else if(ad!=null&&ad.getLevel()==3)//普通教师
        {
            Logger.debug(this.getClass(),String.format("current administrator is teacher"));
            String username = ad.getUsername();
            Teacher t =teacherService.getTeacherByUsername(username);
            if(t!=null)
            {
                empno = t.getEmpno();
            }
        }else if(ad!=null&&ad.getLevel()==2) //院系领导
        {
            Logger.debug(this.getClass(),String.format("current administrator is department leader"));
            String username = ad.getUsername();
            Teacher t =teacherService.getTeacherByUsername(username);
            if(t!=null)
            {
                empno = t.getEmpno();
            }
        }
        tree = scoreService.findCourseTree(empno);
        return "success";
    }

    @Action(value="score",results={@Result(name="success",location="score.jsp")})
    public String score()
    {
        Map<String,Object> params = new HashMap<String, Object>();
        params.put("courseCode",code);
        Page<Score> page = scoreService.queryScore(params,-1,-1,getRequest());
        scores = page.getRecords();
        return "success";
    }

    @Action(value="fill-score",results={@Result(name="success",location="score.jsp")})
    public void fillScore()
    {
        scoreService.fillScore(id,score);
        PrintWriter pw = null;
        try {
            pw = getResponse().getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        pw.print("success");
        pw.flush();
        pw.close();
    }



    public ScoreService getScoreService() {
        return scoreService;
    }

    public void setScoreService(ScoreService scoreService) {
        this.scoreService = scoreService;
    }

    public Map<String, List<Courses>> getTree() {
        return tree;
    }

    public void setTree(Map<String, List<Courses>> tree) {
        this.tree = tree;
    }

    public CacheAble<Object, Object> getCacheAble() {
        return cacheAble;
    }

    public void setCacheAble(CacheAble<Object, Object> cacheAble) {
        this.cacheAble = cacheAble;
    }

    public TeacherService getTeacherService() {
        return teacherService;
    }

    public void setTeacherService(TeacherService teacherService) {
        this.teacherService = teacherService;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public List<Score> getScores() {
        return scores;
    }

    public void setScores(List<Score> scores) {
        this.scores = scores;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public double getScore() {
        return score;
    }

    public void setScore(double score) {
        this.score = score;
    }
}
